package com.ruoyi.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.customer.domain.TMemoRecord;
import com.ruoyi.customer.domain.TotalComster;
import com.ruoyi.customer.domain.bo.TMemoRecordBo;
import com.ruoyi.customer.domain.vo.TMemoRecordVo;
import com.ruoyi.customer.mapper.TMemoRecordMapper;
import com.ruoyi.customer.service.ITMemoRecordService;
import com.sun.org.apache.xpath.internal.operations.String;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 备忘信息记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-19
 */
@RequiredArgsConstructor
@Service
public class TMemoRecordServiceImpl implements ITMemoRecordService {

    private final TMemoRecordMapper baseMapper;

    /**
     * 查询备忘信息记录
     */
    @Override
    public TMemoRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询备忘信息记录列表
     */
    @Override
    public TableDataInfo<TMemoRecordVo> queryPageList(TMemoRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TMemoRecord> lqw = buildQueryWrapper(bo);
        Page<TMemoRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询备忘信息记录列表
     */
    @Override
    public List<TMemoRecordVo> queryList(TMemoRecordBo bo) {
        LambdaQueryWrapper<TMemoRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TMemoRecord> buildQueryWrapper(TMemoRecordBo bo) {
        LambdaQueryWrapper<TMemoRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(TMemoRecord::getUserId, LoginHelper.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getContent()), TMemoRecord::getContent, bo.getContent());
        lqw.eq(bo.getNoticeTime() != null, TMemoRecord::getNoticeTime, bo.getNoticeTime());
        lqw.eq(bo.getIsOpen() != null, TMemoRecord::getIsOpen, bo.getIsOpen());
        lqw.eq(bo.getCreateId() != null, TMemoRecord::getCreateId, bo.getCreateId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TMemoRecord::getStatus, bo.getStatus());
        lqw.like(StringUtils.isNotBlank(bo.getCustomerTel()), TMemoRecord::getCustomerTel, bo.getCustomerTel());
        lqw.like(StringUtils.isNotBlank(bo.getCustomerName()), TMemoRecord::getCustomerName, bo.getCustomerName());
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), TMemoRecord::getUserName, bo.getUserName());
        lqw.ge(StringUtils.isNotBlank(bo.getStartTime()), TMemoRecord::getNoticeTime, bo.getStartTime());
        lqw.le(StringUtils.isNotBlank(bo.getEndTime()), TMemoRecord::getNoticeTime, bo.getEndTime());
        lqw.orderByDesc(BaseEntity::getCreateTime);
        return lqw;
    }

    /**
     * 新增备忘信息记录
     */
    @Override
    public Boolean insertByBo(TMemoRecordBo bo) {
        TMemoRecord add = BeanUtil.toBean(bo, TMemoRecord.class);
        validEntityBeforeSave(add);
        add.setUserId(LoginHelper.getUserId());
        add.setCreateId(LoginHelper.getUserId());
        add.setUserName(LoginHelper.getLoginUser().getNickName());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改备忘信息记录
     */
    @Override
    public Boolean updateByBo(TMemoRecordBo bo) {
        TMemoRecord update = BeanUtil.toBean(bo, TMemoRecord.class);
        validEntityBeforeSave(update);
        update.setIsOpen(0);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TMemoRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除备忘信息记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
